<template>
  <div>
    <card-view/>
    <a-card :bordered="false" style="margin-top: 16px" title="历史消费记录">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="记录类型">
                <a-select
                    v-model="queryParam.recordType"
                    placeholder="请选择">
                  <a-select-option
                      v-for="(type, i) in types"
                      :key="i">
                    {{ type }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="起点或终点">
                <a-select
                    v-model="queryParam.stationId"
                    placeholder="请选择">
                  <a-select-option
                      v-for="station in stations"
                      :key="station.stationId">
                    {{ station.stationName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="停车时间">
                  <a-range-picker @change="onChange"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons"
                  :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a @click="toggleAdvanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!--      <div class="table-operator">-->
      <!--        <a-button type="primary" icon="plus" @click="$refs.addBike.add()">新建</a-button>-->
      <!--        <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>-->
      <!--        <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">-->
      <!--          <a-menu slot="overlay">-->
      <!--            <a-menu-item key="remove">-->
      <!--            </a-menu-item>-->
      <!--          </a-menu>-->
      <!--          <a-button style="margin-left: 8px">-->
      <!--            批量操作-->
      <!--            <a-icon type="down"/>-->
      <!--          </a-button>-->
      <!--        </a-dropdown>-->
      <!--      </div>-->

      <s-table
          ref="table"
          size="default"
          rowKey="key"
          :columns="columns"
          :data="loadData"
          :alert="options.alert"
          :rowSelection="options.rowSelection"
          showPagination="auto"
      >
      <span slot="recordType" slot-scope="text">
        <a-tag :color="text | typeColorFilter">{{ text | typeTextFilter }}</a-tag>
      </span>
        <span slot="money" slot-scope="text, record">{{ text }} 元</span>
        <span slot="action" slot-scope="text, record">
        <a @click="handleDetail(record)">详情</a>
      </span>
      </s-table>
      <detail-card-record ref="detailCardRecord" :types="types"></detail-card-record>
    </a-card>
  </div>
</template>

<script>
import {STable} from '@/components'
import moment from "moment";
import CardView from "./modules/card/CardView"
import DetailCardRecord from "./modules/card/DetailCardRecord";
import {queryCardRecord} from "@/api/bike/cardRecord"
import {listStation} from "@/api/system/station";

let that
let typeMap = {
  0: {
    text: '消费',
    color: 'blue',
  },
  1: {
    text: '充值',
    color: 'green',
  },
  2: {
    text: '违约',
    color: 'red',
  },
}

export default {
  name: 'CardList',
  components: {
    STable,
    CardView,
    DetailCardRecord,
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      types: ['消费', '充值', '违约'],
      card: null,
      stations: [],
      // 表头
      columns: [
        {
          title: '租借卡号',
          dataIndex: 'cardNumber',
          align: 'center'
        },
        {
          title: '记录类型',
          dataIndex: 'recordType',
          sorter: true,
          align: 'center',
          scopedSlots: {customRender: 'recordType'},
        },
        {
          title: '金额',
          dataIndex: 'money',
          sorter: true,
          align: 'center',
          scopedSlots: {customRender: 'money'},
        },
        {
          title: '出发站',
          dataIndex: 'begStationName',
          align: 'center',
          scopedSlots: {customRender: 'begStationName'},
        },
        {
          title: '终点站',
          dataIndex: 'endStationName',
          align: 'center',
          scopedSlots: {customRender: 'endStationName'},
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收

        const queryJson = {'queryParam': JSON.stringify(this.queryParam)}
        return queryCardRecord(Object.assign(queryJson, parameter))
            .then(res => {
              return res.result
            })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: {
          show: true, clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true
    }
  },
  filters: {
    typeTextFilter (type) {
      if (typeMap[type] === undefined) {
        return ''
      }
      return typeMap[type].text
    },
    typeColorFilter (type) {
      if (typeMap[type] === undefined) {
        return ''
      }
      return typeMap[type].color
    },
  },
  beforeCreate () {
    that = this
  },
  created () {
    this.tableOption()
    this.listStation()
  },
  methods: {
    listStation () {
      this.stations = []
      listStation().then(res => {
        if (res.message !== 'error') {
          this.stations = res.result
        } else {
          this.$message.error('查询站点信息失败')
        }
      }).catch(err => {
        this.$message.error('查询站点信息失败')
      })
    },
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: {
            show: true, clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    onChange (date, dateString) {
      this.queryParam.begDate = dateString[0]
      this.queryParam.endDate = dateString[1]
    },
    handleDetail (record) {
      this.$refs.detailCardRecord.show(record)
    },
  }
}
</script>
